package cn.edu.nuc.ssm.controller;



import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.ssm.entity.Admin;
import cn.edu.nuc.ssm.entity.Goods;
import cn.edu.nuc.ssm.services.interfaces.GoodsService;



@Controller
public class GoodsController {

	@Autowired
	private GoodsService goodsService;
	
	@RequestMapping(value="/adminadd",method=RequestMethod.GET)
	public String addinof(){		
		return "admin-add";
	}
	
	@RequestMapping(value="/adminadd",method=RequestMethod.POST)
	public String add(Goods goods,HttpSession session){
		Admin admin = (Admin)session.getAttribute("admin");
		int ref= goodsService.add(goods);
		
		
		return "redirect:/a/" + admin.getAid() + "/home";
	}
	@RequestMapping(value="/a/{gid}/edit",method=RequestMethod.GET)
	public String toedit(
			@PathVariable("gid") int gid,
			Model model
			){	
		
		Goods goods = goodsService.findById(gid);
		model.addAttribute("goods", goods);
		return "admin-edit";
	}
	@RequestMapping(value="/a/{gid}/edit",method=RequestMethod.POST)
	public String edit(
			Goods goods,
			HttpSession session
			){			
		Admin admin = (Admin)session.getAttribute("admin");
		int ref= goodsService.update(goods);
		
		
		return "redirect:/a/" + admin.getAid() + "/home";
	}
	@RequestMapping(value="/a/{gid}/delt",method=RequestMethod.GET)
	public String delt(
			@PathVariable("gid") int gid,
			Model model,
			HttpSession session
			){	
		Admin admin = (Admin)session.getAttribute("admin");
	
		 int ref= goodsService.delt(gid);
		
		
		return "redirect:/a/" + admin.getAid() + "/home";
	}
}
